package com.powernode.controller;

import com.powernode.framework.Result;
import com.powernode.framework.ResultBean;
import com.powernode.framework.ResultUtil;
import com.powernode.pojo.DormitoryAdmin;
import com.powernode.service.DailyReportService;
import com.powernode.service.DormitoryAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycc
 * @Date: 2023/2/14 12:17
 */
@RestController
@RequestMapping("/api/dormitoryAdmin")
public class DormitoryAdminController {

    @Autowired
    DormitoryAdminService dormitoryAdminService;

    @Autowired
    DailyReportService dailyReportService;

    @PostMapping("/save")
    public Result<String> modify(@RequestBody DormitoryAdmin dormitoryAdmin) {
        int count = dormitoryAdminService.checkIsbn(dormitoryAdmin);
        if(count>0){
            return ResultUtil.fail("该卡号已存在！");
        }
        if(dormitoryAdmin.getId()!=null){
            dormitoryAdminService.modify(dormitoryAdmin);
        }else{
            dormitoryAdminService.add(dormitoryAdmin);
        }

        return ResultUtil.ok();
    }
    @PostMapping("/remove")
    public Result<String> remove(@RequestBody Integer[] ids) {
        dormitoryAdminService.removes(ids);
        return ResultUtil.ok();
    }

    @GetMapping("/list")
    public ResultBean<List<DormitoryAdmin>> getPageResult(
            @RequestParam(required = false) String studentNumber,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String site,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {

        Map<String, Object> param = new HashMap<>(16);

        // 计算起始行号
        int offset = (page - 1) * limit;
        int rows = limit;

        param.put("studentNumber",studentNumber);
        param.put("name",name);
        param.put("site",site);
        param.put("offset", offset);
        param.put("rows", rows);

        // 统计记录数
        int totalRows = dormitoryAdminService.count(param);

        // 获取当前页结果集
        List<DormitoryAdmin> entities = dormitoryAdminService.getPageResult(param);

//        for (Doctor entity : entities) {
//            if (card001Service.selectCardToday(entity.getDocid())!=null){
//                entity.setIspost(1);
//            }else {
//                entity.setIspost(2);
//            }
//        }
        return new ResultBean<>(0, "查询成功", totalRows, entities);

    }


}
